﻿#include <iostream>
#include "InlineHook32.h"
#include "InlineHook64.h"

using pfnOldGetTickCount64_t = DWORD (WINAPI*)(VOID);
pfnOldGetTickCount64_t pOldGetTickCount64{};

ULONGLONG WINAPI My_GetTickCount64(VOID) {
	std::cout << "Hook GetTickCount()" << std::endl;

	/*
	 * 这样容易存在线程安全问题, 不推荐.
	 * ih32_getTickCount64.unhook();
	 * ULONGLONG ret = GetTickCount64();
	 * ih32_getTickCount64.hook();
	 * 
	 * 建议: 调用 pOldGetTickCount64();
	*/

	return 0;
}

void test1() {

	{
		InlineHook32 ih32_getTickCount64(GetTickCount64, My_GetTickCount64, 5);
		pOldGetTickCount64 = (pfnOldGetTickCount64_t)ih32_getTickCount64.getOldFun();
		ih32_getTickCount64.hook();
		std::cout << GetTickCount64() << std::endl;
		std::cout << pOldGetTickCount64() << std::endl;
	}

	std::cout << GetTickCount64() << std::endl;
}


void fun1() {
	std::cout << "正常的func()" << std::endl;

}

void my_fun1() {
	std::cout << "被hook的func()" << std::endl;
}

void test2() {
	InlineHook32 ih32_fun1(fun1, my_fun1);
	using pfnFun = void(*)();
	fun1();
	ih32_fun1.hook();
	fun1();

}

void test3() {
	InlineHook64 ih64_getTickCount64(GetTickCount64, My_GetTickCount64);
	ih64_getTickCount64.hook();
	std::cout << GetTickCount64() << std::endl;
	ih64_getTickCount64.unhook();
	std::cout << GetTickCount64() << std::endl;
}

int main() {

#ifdef _WIN64
	/* 64位测试 */
	test3();
#else
	/* 32位测试 */
	test1();
	test2();
#endif

	std::cout << "end...\r\n";
	return 0;
}



